<?php
include_once("ConnectionManager.php");
function createComment($pro_id, $user_id, $content, $parent){
    if(is_null($parent)){
        $query = "INSERT INTO MITI_COMMENT(PRO_ID, USER_ID, COM_CONTENT, COM_STATUS, COM_CREATIONDATE) VALUES($pro_id,$user_id,'".htmlspecialchars($content, ENT_QUOTES)."',FALSE,CONCAT(CURDATE(),' ',CURTIME()))";
    }else{
        $query = "INSERT INTO MITI_COMMENT(COM_COM_ID, PRO_ID, USER_ID, COM_CONTENT, COM_STATUS, COM_CREATIONDATE) VALUES($parent,$pro_id,$user_id,'".htmlspecialchars($content, ENT_QUOTES)."',FALSE,CONCAT(CURDATE(),' ',CURTIME()))";
    }
    
    ConnectionManager::getInstance()->setQuery($query);
    $result = ConnectionManager::getInstance()->query();
    return $result;
}

function getAllCommentOfProduct($pro_id){
    $query = "SELECT * FROM MITI_COMMENT WHERE PRO_ID = $pro_id AND COM_STATUS = TRUE ORDER BY COM_CREATIONDATE DESC";
    ConnectionManager::getInstance()->setQuery($query);
    return ConnectionManager::getInstance()->query();
}

function countAllRootCommentOfProduct($pro_id){
    $query = "SELECT COUNT(COM_ID) FROM MITI_COMMENT WHERE PRO_ID = $pro_id AND COM_STATUS = TRUE AND COM_COM_ID IS NULL ORDER BY COM_CREATIONDATE DESC";
    ConnectionManager::getInstance()->setQuery($query);
    $result = ConnectionManager::getInstance()->query();
    $row = mysql_fetch_array($result);
    return $row[0];
}

function getAllApproveFirstCommentOfProduct($pro_id, $start_record, $qty){
    if(!is_null($qty)){
        $start_record = $start_record -1;
        $query = "SELECT * FROM MITI_COMMENT WHERE PRO_ID = $pro_id AND COM_COM_ID IS NULL AND COM_STATUS = TRUE ORDER BY COM_CREATIONDATE DESC LIMIT $start_record,$qty";    
    }else{
        $query = "SELECT * FROM MITI_COMMENT WHERE PRO_ID = $pro_id AND COM_COM_ID IS NULL AND COM_STATUS = TRUE ORDER BY COM_CREATIONDATE DESC";
    }
    ConnectionManager::getInstance()->setQuery($query);
    return ConnectionManager::getInstance()->query();
}

function getAllFirstCommentOfProduct($pro_id, $start_record, $qty){
    if(!is_null($qty)){
        $start_record = $start_record -1;
        $query = "SELECT * FROM MITI_COMMENT WHERE PRO_ID = $pro_id AND COM_COM_ID IS NULL ORDER BY COM_CREATIONDATE DESC LIMIT $start_record,$qty";    
    }else{
        $query = "SELECT * FROM MITI_COMMENT WHERE PRO_ID = $pro_id AND COM_COM_ID IS NULL ORDER BY COM_CREATIONDATE DESC";
    }
    ConnectionManager::getInstance()->setQuery($query);
    return ConnectionManager::getInstance()->query();
}

function hasChildComment($com_id){
    $query = "SELECT COUNT(COM_ID) FROM MITI_COMMENT WHERE COM_COM_ID = $com_id AND COM_STATUS = TRUE";
    ConnectionManager::getInstance()->setQuery($query);
    $result = ConnectionManager::getInstance()->query();	$row = mysql_fetch_array($result);
    return $row[0] > 0;
}

function countChildComment($com_id){
    $query = "SELECT COUNT(COM_ID) FROM MITI_COMMENT WHERE COM_COM_ID = $com_id AND COM_STATUS = TRUE";
    ConnectionManager::getInstance()->setQuery($query);
    $result = ConnectionManager::getInstance()->query();
    $row = mysql_fetch_array($result);
    return $row[0];
}

function getAllChildrenComment($com_id){
    $query = "SELECT * FROM MITI_COMMENT WHERE COM_COM_ID = $com_id AND COM_STATUS = TRUE ORDER BY COM_CREATIONDATE DESC";
    ConnectionManager::getInstance()->setQuery($query);
    return ConnectionManager::getInstance()->query();
}

function getAllChildrenCommentAllStatus($com_id){
    $query = "SELECT * FROM MITI_COMMENT WHERE COM_COM_ID = $com_id ORDER BY COM_CREATIONDATE DESC";
    ConnectionManager::getInstance()->setQuery($query);
    return ConnectionManager::getInstance()->query();
}

function approveComment($com_id){
    $query = "UPDATE MITI_COMMENT SET COM_STATUS = 1 WHERE COM_ID = $com_id";
    ConnectionManager::getInstance()->setQuery($query);
    return ConnectionManager::getInstance()->query();
}

function disableComment($com_id){
    $query = "UPDATE MITI_COMMENT SET COM_STATUS = 0 WHERE COM_ID = $com_id";
    ConnectionManager::getInstance()->setQuery($query);
    return ConnectionManager::getInstance()->query();
}

//Bid-Auction
function getBidsOfSpecialProduct($spro_id,$start_record,$record_per_page){
    if(!is_null($record_per_page)){
        $start_record = $start_record -1;
        $query = "SELECT * FROM MITI_COMMENT WHERE SPRO_ID = $spro_id ORDER BY COM_CREATIONDATE DESC LIMIT $start_record,$record_per_page";
    }else{
        $query = "SELECT * FROM MITI_COMMENT WHERE SPRO_ID = $spro_id ORDER BY COM_CREATIONDATE DESC";
    }    
    ConnectionManager::getInstance()->setQuery($query);
    $result = ConnectionManager::getInstance()->query();
    return $result;
}

function countAllBidOfSpecialProduct($spro_id){
    $query = "SELECT COUNT(COM_ID) FROM MITI_COMMENT WHERE SPRO_ID = $spro_id AND COM_STATUS = TRUE AND COM_COM_ID IS NULL ORDER BY COM_CREATIONDATE DESC";
    ConnectionManager::getInstance()->setQuery($query);
    $result = ConnectionManager::getInstance()->query();
    $row = mysql_fetch_array($result);
    return $row[0];
}

function createBid($spro_id,$user,$content){
    $query = "INSERT INTO MITI_COMMENT(SPRO_ID, USER_ID, COM_CONTENT, COM_STATUS, COM_CREATIONDATE) VALUES($spro_id,$user,'".htmlspecialchars($content, ENT_QUOTES)."',TRUE,CONCAT(CURDATE(),' ',CURTIME()))";
    ConnectionManager::getInstance()->setQuery($query);
    $result = ConnectionManager::getInstance()->query();
    return $result;
}
?>

